package com.cheneyjoo.controller;

import com.cheneyjoo.factory.ClassFactory;
import com.cheneyjoo.model.STUDENT;
import com.cheneyjoo.service.IStudentService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 操作学生信息接口类
 */
public class StudentController {
    private final IStudentService studentService;
    public StudentController() {
        studentService= ClassFactory.getStudentService();
    }

    /**
     * 添加学生
     * @param student 学生信息
     * @return 是否成功
     */
    public boolean addStudent(STUDENT student){
        return studentService.addStudent(student);
    }

    /**
     * 修改学生信息
     * @param student 学生信息
     * @return 是否成功
     */
    public boolean updateStudent(STUDENT student){
        return  studentService.updateStudent(student);
    }

    /**
     * 删除学生
     * @param ids 学生ID
     * @return 是否成功
     */
    public boolean deleteStudent(String ids){
        AtomicReference<String> newIds= new AtomicReference<>("");
        Arrays.stream(ids.split(",")).map(s->"'"+s+"'").forEach(s->{
            newIds.set(newIds.get()+s+",");
        });
        ids=newIds.get().substring(0,newIds.get().length()-1);
        return studentService.deleteStudent(ids);
    }

    /**
     * 获取学生
     * @return 学生信息
     */
    public STUDENT getStudent(int id){
        return studentService.getStudent(id);
    }

    /**
     * 查询学生列表
     * @param where 查询条件
     * @param currPage  当前页
     * @param pageSize  每页条数
     * @return 学生列表结果集 和页数 总行数
     */
    public Map<String,Object> getStudents(Map<String,String> where,int currPage,int pageSize){
        return studentService.getStudents(pageSize,currPage,where);
    }

    public List<STUDENT> checkStudentHaveClass(String ids){
        AtomicReference<String> newIds= new AtomicReference<>("");
        Arrays.stream(ids.split(",")).map(s->"'"+s+"'").forEach(s->{
            newIds.set(newIds.get()+s+",");
        });
        ids=newIds.get().substring(0,newIds.get().length()-1);
        return studentService.checkStudentHaveClass(ids);
    }
}
